<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 审批列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-delete"
            class="handle-del mr10"
            @click="delAllSelection"
        >批量通过</el-button>
        <el-input v-model='query.aname'  placeholder="专辑名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="aid" label="专辑编号" width="55" align="center"></el-table-column>
        <el-table-column prop="aname" label="专辑名"></el-table-column>
          <el-table-column prop="author" label="作者"></el-table-column>
          <el-table-column prop="tname" label="类型"></el-table-column>
          <el-table-column prop="cover" label="封面图">
              　<template slot-scope="scope">
              　　　　<img :src="require('../../assets/img/'+scope.row.cover)"  width="40" height="40" class="head_pic"/>
              　　</template>
          </el-table-column>
          <el-table-column   prop="updstate" label="更新状态" :formatter='formupdstate'></el-table-column>
          <el-table-column   prop="state" label="审批状态" :formatter='formstate'></el-table-column>
          <el-table-column   prop="remark" label="备注"  width='300'></el-table-column>
          <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
                type="text"
                @click="handleEdit( scope.row)"
            >通过
            </el-button>
            <el-button
                type="text"
                class="red"
                @click="handleDelete(scope.row)"
            >不通过
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
<!--        // layout prev 左箭头 pager 页数 next 右箭头
        //@current-change 页数改变时间 有一个参数 就是当前页数
        //total 条数 会根据总条数和每页显示的条数进行 分页-->
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="preNext"
            :page-size='pagesize'
            :total="total">
        </el-pagination>
          <el-dialog title="不通过原因" :visible.sync="editVisible" width="30%">
              <el-form ref="form" :model="form" label-width="70px">
                  <el-form-item label="原因">
                      <el-input type='text' v-model="form.remark" ></el-input>
                  </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
          </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "albumtable",
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 6, pagesize:5,
      form: {},
      idx: -1,
      id: -1,
       aname:undefined

    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
        //员工列表
     this.$http({
       url:'http://localhost:8090/album/selectAlbum',
       method:'post',
       params:{pageNum:1,pageSize:5,aname:this.query.aname}
     }).then(response=>{
         //console.log(response)
       //console.log(response)
       this.tableData=response.list
         this.total=response.total
         this.pagesize=response.pageSize
     })
        //获取类型列表
        this.$http({
            url:'http://localhost:8090/type/selectType',
            method:'post',
        }).then(res=>{
            this.type=res
        })
    },formupdstate(row){return row.updstate===1?'连载中':'已完结'},formstate(row){
          if(row.state==1){
              return '成功';
          }else if(row.state==2){
              return '失败'
          }else {
              return '待审批'
          }
      }//数据转换
      // 通过方法
      ,handleEdit(row){
        if(row.state==3){
        this.$http({
            method:'post',
            url:'approval/approvalAdd',
            params:{aname:sessionStorage.getItem('eusername'),albname:row.aname,state:1}
        }).then((res)=>{
            if(res==true){
                this.$message.success('通过成功')
                this.getData()
            }else {
                this.$message.error('操作失败')
            }
        })
        }else {
            this.$message.error('已经审批过不能重复审批')
        }
    }, // 触发搜索按钮
    handleSearch() {
      this.$http({
        url:'http://localhost:8090/album/selectAlbum',
        method:'post',
        params:{pageNum:1,pageSize:5,aname:this.query.aname}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })

    }
    ,
    // 不通过方法
    handleDelete(row) {//显示dalog框并赋值
        if(row.state==3){
            this.editVisible=true
            this.albname=row.albname
        }else {
            this.$message.error('已经审批过不能重复审批')

        }

    },
      // 多选操作
      handleSelectionChange(val) {
          this.multipleSelection = val;
      },
    delAllSelection() {
        let str="";
        let albnames="";
        let state=new Array();
        let info=true;
        for (let i=0;i<this.multipleSelection.length;i++){
            str+=this.multipleSelection[i].aid+","
            albnames+=this.multipleSelection[i].aname+","
            state.push(this.multipleSelection[i].state)
            if(state[i]!=3){
                info=false;
            }
        }
        //先校验选选中的数据是否已经审批
       if(info==true){
        this.$http({
            url:'/approval/approvalAdds',
            method:'post',
            params:{str:str,albnames:albnames,state:1,aname:sessionStorage.getItem('eusername')}
        }).then(res=>{
            if(res==true){
                this.$message.success('成功')
                this.getData()
            }else {
                this.$message.error('失败')
            }

        })
       }else {
           this.$message.error('已经审批过不能重复审批')
       }
    },//不通过的提交方法
      submit(){//提交
        this.$http({
            method:'post',
            url:'approval/approvalAdd',
            params:{aname:sessionStorage.getItem('eusername'),albname:this.aname,remark:this.form.remark,state:2}
        }).then((res)=>{
            if(res==true){
                this.$message.success('成功')
            }else {
                this.$message.error('失败')
            }
        })
      }
      ,
    // 分页导航
    handlePageChange() {
      this.getData();
    },preNext(val){
      this.$http({
        url:'http://localhost:8090/album/selectAlbum',
        method:'post',
        params:{pageNum:val,pageSize:5,aname:this.query.aname}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
